package com.springboot.config;

import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.TreeMap;

import javax.annotation.Resource;

import org.apache.shiro.mgt.SecurityManager;
import org.apache.shiro.spring.web.ShiroFilterFactoryBean;
import org.apache.shiro.web.mgt.DefaultWebSecurityManager;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.UrlBasedCorsConfigurationSource;
import org.springframework.web.filter.CorsFilter;
import org.springframework.web.servlet.config.annotation.CorsRegistration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import com.springboot.bean.MyRealm;

import at.pollux.thymeleaf.shiro.dialect.ShiroDialect;

@Configuration
public class ApplicationConfig implements WebMvcConfigurer {

    @Resource
    private MyRealm myRealm;

    @Bean
    public ShiroFilterFactoryBean getShiroFilterFactoryBean() {
        SecurityManager manager = new DefaultWebSecurityManager(myRealm);
        ShiroFilterFactoryBean factoryBean = new ShiroFilterFactoryBean();
        // 设置安全管理器
        factoryBean.setSecurityManager(manager);
        Map<String, String> filterMap = new LinkedHashMap<>();
        filterMap.put("/userview/**", "anon");
        filterMap.put("/company/**", "roles[role1]"); // 公司操作
        filterMap.put("/project/getmanholes", "anon"); // 沙井查询
        filterMap.put("/project/**", "roles[role4]"); // 项目操作
        filterMap.put("/manhole/**", "roles[role4]"); // 沙井操作
        filterMap.put("/pipe/**", "roles[role4]"); // 管道操作
        filterMap.put("/item/**", "roles[role4]"); // 图片操作
        filterMap.put("/operator/**", "roles[role2]"); // 沙井操作
        filterMap.put("/markinfo/**", "roles[role4]"); // 评分操作
        filterMap.put("/message/**", "roles[role4]"); // 消息操作
        filterMap.put("/userinfo/showlist", "roles[role2]"); // 人员管理
        filterMap.put("/userinfo/update", "roles[role2]"); // 更新人员
        filterMap.put("/userinfo/center", "roles[role4]"); // 个人中心
        filterMap.put("/downfile", "roles[role4]"); // 下载操作
        factoryBean.setFilterChainDefinitionMap(filterMap);

        // 配置跳转的登录页面
        factoryBean.setLoginUrl("/userview/loginview");
        // 设置未授权提示页面
        factoryBean.setUnauthorizedUrl("/authorize");
        return factoryBean;
    }

    /**
     * 定义识图控制器
     */
    public void addViewControllers(ViewControllerRegistry registry) {
        registry.addViewController("**/authorize").setViewName("common/authorize");
        registry.addViewController("**/success").setViewName("common/success");
        registry.addViewController("**/failure").setViewName("common/failure");

        registry.addViewController("**/loginview").setViewName("userview/loginview");
        registry.addViewController("**/resetview").setViewName("userview/resetview");
        registry.addViewController("**/completes").setViewName("userview/completes");

        registry.addViewController("/operator/insertview").setViewName("operator/insert");
        registry.addViewController("/userinfo/center").setViewName("userinfo/center");
    }

    @Bean
    public ShiroDialect shiroDialect() {
        return new ShiroDialect();
    }

    /**
     * 跨域访问设置
     */
    public void addCorsMappings(CorsRegistry registry) {
        CorsRegistration registration = registry.addMapping("/**");
        registration.allowedOrigins("*");
        registration.allowedMethods("GET", "POST", "PUT", "DELETE");
        registration.allowCredentials(true);
        registration.maxAge(3600);
    }

}
